package threadcoreknowledge;

public class Main implements Runnable {

   Object lock = new Object();
    public static void main(String[] args) {
        Main runnable = new Main();


        Thread thread = new Thread(runnable);
        System.out.println(Thread.currentThread().getName()+"线程在运行,------得到thread0状态:"+thread.getState());
        thread.start();
        System.out.println(Thread.currentThread().getName()+"线程在运行,------得到thread0状态:"+thread.getState());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"线程在运行,------得到thread0状态:"+thread.getState());

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"线程在运行,------得到thread0状态:"+thread.getState());

    }


    @Override
    public void run() {
//        syn1();
        syn2();
//        syn3();
    }

    private void syn1() {
        try {
            Thread.sleep(100);
            System.out.println("--------111--------");
            this.wait();// 要使用这把锁,就必须获取到这把锁
//            lock.wait();
            System.out.println("--------222--------");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void syn2() {
                synchronized (this) {
            try {
                Thread.sleep(1000);
                System.out.println("--------111--------");
                this.wait();
                System.out.println("--------222--------");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private synchronized void syn3() {
        try {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"----"+Thread.currentThread().getState()+"----this的hashCode是"+System.identityHashCode(this)+"----this is "+this);
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
